<!--  -->
<template>
  <div class="tabs">
    <el-tag
      :key="index"
      v-for="(tag, index) in tags"
      :closable="tag.name !== 'home'"
      :disable-transitions="false"
      @close="handleClose(tag, index)"
      @click="changeMenu(tag)"
      :effect="$route.name === tag.name ? 'dark' : 'plain'"
      >{{ tag.label }}</el-tag
    >
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex';
export default {
  data() {
    return {};
  },

  components: {},

  computed: {
    ...mapState({
      tags: state => state.tab.tabsList
    })
  },

  mounted() {},

  methods: {
    ...mapMutations({
      close: 'closeTab'
    }),
    handleClose(tag, index) {
      let length = this.tags.length - 1;
      //vuex调方法的另一种写法
      this.close(tag);
      // 如果关闭的标签不是当前路由的话，就不跳转
      if (this.$route.name !== tag.name) {
        return;
      }
      // 关闭的标签是最右边的话，往左边跳转一个
      if (index === length) {
        this.$router.push({ name: this.tags[index - 1].name });
      } else {
        // 否则往右边跳转
        this.$router.push({ name: this.tags[index].name });
      }
    },
    // 切换标签
    changeMenu(tag) {
      this.$router.push(tag.path);
      this.$store.commit('selectMenu', tag);
    }
  }
};
</script>

<style lang="scss" scoped>
.tabs {
  padding-left: 20px;
  .el-tag {
    margin-right: 15px;
    cursor: pointer;
  }
}
</style>
